package classes.graphicalEngine
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	import mx.controls.Menu;
	import mx.controls.TextArea;
	import mx.events.CloseEvent;
	import mx.events.MenuEvent;
	import mx.managers.PopUpManager;
	import components.GraphicalEngineComp;

	
	/*
		The base class for all objects in the game.
	*/
	public class GameObject
	{
		// object position
		public var position:Point = new Point(0, 0);
		
		// higher zOrder objects are rendered on top of lower ones
		public var zOrder:int = 0;
		// the bitmap data to display	
		public var graphics:GraphicsResource = null;
		// true if the object is active in the game
		public var inuse:Boolean = false;
		
		public var objectName:String;
		
		public function GameObject()
		{
			
		}
		
		public function startupGameObject(graphics:GraphicsResource, position:Point, z:int = 0):void
		{
			if (!inuse)
			{
				this.graphics = graphics;
				this.zOrder = z;
				this.position = position.clone();
				this.objectName = ResourceManager.getGraphicsResourceName(this.graphics);
				GameObjectManager.needToBeRefreshed = true;
				this.inuse = true;
				GameObjectManager.Instance.addGameObject(this);
			}
		}
		
		public function shutdown():void
		{
			if (inuse)
			{				
				graphics = null;
				inuse = false;
				
				GameObjectManager.Instance.removeGameObject(this);
			}
		}
		
		public function copyToBackBuffer(db:BitmapData):void
		{
			db.copyPixels(graphics.bitmap, graphics.bitmap.rect, position, graphics.bitmapAlpha, new Point(0, 0), true);
		}
		
		public function enterFrame(dt:Number):void
		{

		}
		
		public function click(event:MouseEvent):void
		{
			if (event.localX >= position.x  &&
				event.localX < position.x + graphics.bitmap.width &&
				event.localY >= position.y &&
				event.localY < position.y + graphics.bitmap.height)
			{
				if (GraphicalEngineComp.menuContextuel && GraphicalEngineComp.menuContextuel.isPopUp) 	
					PopUpManager.removePopUp(GraphicalEngineComp.menuContextuel);
				var textArea:TextArea = new TextArea;
				textArea.text = position.toString();
				textArea.height = 100;
				
					
			
				
				
				var item:XML;
				if (objectName == "Cacao2" ||
					objectName == "Latex2" ||
					objectName == "Manioca" ||
					objectName == "Soja")
				{
					item = <test> <item label={objectName}></item> <menuitem type="separator"/> <item label="Action"><item label="Collect"/></item><menuitem type="separator"/><item label="Properties"></item> </test>;
				}
				else if (objectName == "Home")
				{
					item = <test> <item label={objectName}></item> <menuitem type="separator"/> <item label="Action"><item label="Buy"/></item><menuitem type="separator"/><item label="Properties"></item> </test>;
				}
				else {
					item = <test> <item label={objectName}></item> <menuitem type="separator"/><item label="Properties"></item> </test>;
				}
				
				var xmlList:XMLList = XMLList(item);
				GraphicalEngineComp.menuContextuel = Menu.createMenu(null, xmlList, false);
                GraphicalEngineComp.menuContextuel.labelField="@label";
                GraphicalEngineComp.menuContextuel.addEventListener(MenuEvent.ITEM_CLICK,itemClickInfo);
                GraphicalEngineComp.menuContextuel.show(event.stageX,event.stageY);
				/*
				GraphicalEngineComp.menuContextuel = TitleWindow(PopUpManager.createPopUp(GraphicalEngineComp.canvasGraphicalEngine, TitleWindow));
				graphicalEngine.menuContextuel.addChild(GraphicalEngineComp.menuContextuel);
				GraphicalEngineComp.menuContextuel.showCloseButton = true;
				GraphicalEngineComp.menuContextuel.title = position.toString();
				GraphicalEngineComp.menuContextuel.x = event.stageX;
				GraphicalEngineComp.menuContextuel.y = event.stageY;
				GraphicalEngineComp.menuContextuel.addEventListener(CloseEvent.CLOSE, closeHandler);*/
			}
		}
		
	 // The event listener for the itemClick event.
        protected function itemClickInfo(event:MenuEvent):void {
        }

		protected function closeHandler(event:CloseEvent):void{
			PopUpManager.removePopUp(GraphicalEngineComp.menuContextuel);
		}

		public function mouseDown(event:MouseEvent):void
		{
		
		}
		
		public function mouseUp(event:MouseEvent):void
		{
		
		}
		
		public function mouseMove(event:MouseEvent):void
		{
		
		}
	}
}